package com.lkm.app_ui.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.help.HandleDialog;
import com.lkm.app_ui.m.camera.CameraModule;
import com.lkm.app_ui.m.fun.FunItem;
import com.lkm.app_ui.m.fun.OnKeyNumberEvent;
import com.lkm.app_ui.view.choiceCard.ChoiceCard;
import com.lkm.app_ui.view.choiceCard.ChoiceItem;
import com.lkm.app_ui.view.choiceCard.IChoiceItem;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;
import com.lkm.market2_0.netapi.Api;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



public class WifiFragment extends MBaseFragment implements  ChoiceCard.ChoiceCardBC {


    public static WifiFragment getInstance(boolean isAutoGoScan, String cmd) {
        WifiFragment f = new WifiFragment();
        Bundle data = new Bundle();
        data.putBoolean("isAutoGoScan",isAutoGoScan);
        data.putString("cmd", cmd);

        f.setArguments(data);


        return f;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_wifi;
    }

    @Override
    protected boolean configIsLockKey() {
        return true;
    }

    private TextView tv_content;
    private TextView tv_tips;


    WifiFragmentFun wifiFragmentFun ;
    private ChoiceCard mChoiceCard;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv_content = findViewById(R.id.tv_content);
        tv_tips = findViewById(R.id.tv_tips);
        mChoiceCard = findViewById(R.id.ChoiceCard);

        mChoiceCard.init(false, R.layout.view_choice_card, R.layout.item_fun_mini_p, R.layout.item_fun_mini, 5);
        mChoiceCard.setChoiceCardBC(this);
        initDatax();

        reOnKeyNumberEvent();

        if(getArguments().getBoolean("isAutoGoScan")){
            goSacnQRView();
        }else if(!StringHelp.isEmpty(getArguments().getString("cmd",""))){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    wifiFragmentFun.onReadStr(getArguments().getString("cmd",""));
                }
            },1500);
        }

        
        CameraModule cameraModule = (CameraModule) AppConst.getModuleManager().peekModule(CameraModule.class);
        if(cameraModule != null){
            cameraModule.close();
        }
        wifiFragmentFun = new WifiFragmentFun(this);

        tv_tips.setText(Api.host);
    }

    private void initDatax(){
        List<FunItem> funItems = new ArrayList<>();
        funItems.add(new FunItem("0", "0：连接wifi"));
        funItems.add(new FunItem("1", "1：出厂登记"));
        funItems.add(new FunItem("11", "11：上传AD"));
        funItems.add(new FunItem("2", "2：标定台秤-深圳"));
        funItems.add(new FunItem("3", "3：更新apk"));
        funItems.add(new FunItem("33", "33：安装本地apk"));
        funItems.add(new FunItem("4", "4：重新启动app"));
        funItems.add(new FunItem("5", "5：按键类下发"));
        funItems.add(new FunItem("6", "6：关于设备"));




        funItems.add(new FunItem("7", "7：打印机-cm"));
        funItems.add(new FunItem("77", "77：打印机-新"));
        funItems.add(new FunItem("8", "8：加载打印模板"));
        funItems.add(new FunItem("9", "9：退出"));

        ChoiceItem[] cis = new ChoiceItem[funItems.size()];
        int i = 0;
        for (FunItem funItem : funItems) {
            cis[i] = new ChoiceItem(funItem.id, funItem.name);
            cis[i].setTag(funItem);
            i++;
        }

        mChoiceCard.binData(cis);
    }

    public void goSacnQRView() {
//        RouterUINavigation.getInstance().dismiss(WifiFragment.this);
        if (CaptureBarcodeActivity.captureBarcodeActivity == null)
            startActivityForResult(new Intent(getActivity(), CaptureBarcodeActivity.class), 1);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {

                String qrs = data.getStringExtra("data");
                if (qrs != null && !qrs.isEmpty()) {



                    wifiFragmentFun.onReadStr(qrs);


                }

            }
        }

    }

    private void reOnKeyNumberEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnKeyNumberEvent>() {
            @Override
            public Class<OnKeyNumberEvent> getBusEventClass() {
                return OnKeyNumberEvent.class;
            }

            @Override
            public void onReceiver(OnKeyNumberEvent data) {
                if(isExit())return;

                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    if (data.isFinally() ) {
                        mChoiceCard.onItemClick(""+((int)data.getNumber()));
//                        wifiFragmentFun.onNumber(data.getNumber());
                    }
                }
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);

        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }
            @Override
            public void onReceiver(OnSteelyardKeyEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    if(data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_FUN){
                        
                        mChoiceCard.onItemClick(""+((int)OnSteelyardKeyEvent.KEYCODE_9));
                    }
                }
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);

    }

    @Override
    public void onChoiceChangeListener(ChoiceCard cc, IChoiceItem c, int index) {
        if (c.getIsChoice()) {
            FunItem funItem = (FunItem) c.getTag();
            float number = -1;
            try {
                number = Float.parseFloat(funItem.id);
            }catch (Exception e){e.printStackTrace();}
            wifiFragmentFun.onNumber(number);

        }

    }



    public void uiTips(final String s) {
        if (isExit()) return;
        final Activity activity = getActivity();
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ViewUIHelp.showTipsLong(activity, s);
                }
            });
        }
    }


    public void showTipsDialog(String tips){
        new HandleDialog().show(getActivity(), tips, OnSteelyardKeyEvent.KEYCODE_9, "关闭（按9）", null, false);
    }

    public void showContent(String s) {
        tv_content.setText(s);
    }

    public void closeUI() {
        RouterUINavigation.getInstance().dismiss(this);
    }
}
